package com.topboya.linker.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.levelitem.DepartmentGroupBo;
import com.topboya.linker.utils.LogUtils;

import java.util.List;
//自己写的adapter在返回的时候数据老有问题，后面在研究一下
public class SelecteamAdapter extends RecyclerView.Adapter<SelecteamAdapter.ListItemViewHolder> {

    private static final String TAG = "SelecteamAdapter:";
    private List<DepartmentGroupBo> departmetGroupList;

    public SelecteamAdapter(List<DepartmentGroupBo> departmetGroupList) {

        this.departmetGroupList = departmetGroupList;

    }

    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.select_team_item, viewGroup, false);
        return new ListItemViewHolder(itemView);

    }


    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final ListItemViewHolder holder, final int i) {

        LogUtils.v(TAG+"departmetGroupList---:"+departmetGroupList.toString());
        String  departname = departmetGroupList.get(i).getDeparemtname();
        holder.tv_head_name.setText(departname);
        holder.ck_select.setChecked(departmetGroupList.get(i).isCheked());
            if (departmetGroupList.get(i).isSub()){

            holder.tv_head_pic.setVisibility(View.GONE);
            holder.tv_head_num.setVisibility(View.VISIBLE);


        }else {

            String  departnamepic = departname.substring(departname.length()-2);
            holder.tv_head_pic.setVisibility(View.VISIBLE);
            holder.tv_head_num.setVisibility(View.GONE);
            holder.tv_head_pic.setText(departnamepic);


        }
        //点击事件监听
        clickListenter(holder, i);

    }

    private void clickListenter(ListItemViewHolder holder, int i) {
        holder.itemView.setOnClickListener(v -> {

            if (null !=itemClickListener){
                itemClickListener.onClick(holder.itemView,i);
            }
        });

        holder.ck_select.setOnCheckedChangeListener((buttonView, isChecked) -> {

            if (null != itemCheckboxChangeChangeListener){

                itemCheckboxChangeChangeListener.onCheckedChanged(buttonView,isChecked,i);
            }
        });
    }

    @Override
    public int getItemCount() {

        return departmetGroupList == null ? 0 : departmetGroupList.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_head_pic;
        TextView tv_head_name;
        TextView tv_head_num;
        CheckBox ck_select;

        ListItemViewHolder(View view) {

            super(view);
            this.tv_head_pic = view.findViewById(R.id.tv_head_pic);
            this.tv_head_name = view.findViewById(R.id.tv_head_name);
            this.tv_head_num = view.findViewById(R.id.tv_head_num);
            this.ck_select = view.findViewById(R.id.ck_select);

        }

    }


    private OnItemClickListener itemClickListener;

    public void setOnItemClickListener(SelecteamAdapter.OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

    public interface  OnItemClickListener{

        void onClick(View v,int position);
    }

    private OnItemCheckboxChangeListener itemCheckboxChangeChangeListener;

    public void setOnItemCheckboxChangeListener(OnItemCheckboxChangeListener itemCheckboxChangeChangeListener){

        this.itemCheckboxChangeChangeListener = itemCheckboxChangeChangeListener;
    }

    public interface  OnItemCheckboxChangeListener{

        void onCheckedChanged(CompoundButton buttonView, boolean isChecked,int position);
    }



}
